package com.swpu.course_schedule.controller;

import com.swpu.course_schedule.POJO.Course.Classroom;
import com.swpu.course_schedule.POJO.Course.ClassroomCourseArrange;
import com.swpu.course_schedule.POJO.Course.CourseArrange;
import com.swpu.course_schedule.POJO.Course.SingleWeekCourse;
import com.swpu.course_schedule.Util.SessionFactory;
import com.swpu.course_schedule.service.Impl.ClassroomCourseArrangeImpl;
import com.swpu.course_schedule.service.Impl.SingleWeekCourseImpl;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;

@Controller
@RequestMapping("/course_schedule")
public class CourseScheduleController {

    private static final Logger logger = LoggerFactory.getLogger(CourseScheduleController.class);

    /**
     * 显示一个周次内所有教室的课程信息
     * @param model model
     * @return 课程页面
     */
    @RequestMapping("/classroom")
    public String classroom(Model model){
        System.out.println("----selectAllClassroom-----");
        SqlSession session = SessionFactory.initSqlSessoin();

        // 获取教室信息和课程安培
        List<Classroom> classrooms = session.selectList("selectAllClassroom");
        List<CourseArrange> courseArranges = session.selectList("selectAllCourseArrange");

        // 封装教室的课程信息
        ClassroomCourseArrangeImpl arrange = new ClassroomCourseArrangeImpl();
        List<ClassroomCourseArrange> roomArranges = arrange.getClassroomArrangeInfo(
                classrooms,courseArranges
        );

//        printClassroomCourseArrange(roomArranges);

        model.addAttribute("ClassroomCourseArrange", roomArranges);

        int week = 1;
        model.addAttribute("week", week);

        System.out.println("over");
        return "Course/course_schedule";
    }

    private void printClassroomCourseArrange(List<ClassroomCourseArrange> roomArranges){
        for (ClassroomCourseArrange cca :
                roomArranges) {
            System.out.println("this is classroom: " + cca.getClassroom().getRID());
            for (CourseArrange ca :
                    cca.getCourseArranges()) {
                System.out.println(ca.toString());
            }
        }
    }

    @RequestMapping("test")
    public String test(Model model){

        return "test2";
    }

    @RequestMapping("QueryCourseByRidAndWeek")
    public String QueryCourseByRidAndWeek(){
        return "Course/QueryCourseByRidAndWeek";
    }

    @RequestMapping("single_courses")
    public String singleCourse(Model model, int week, int rid){
        SingleWeekCourse weekCourse =
                new SingleWeekCourseImpl().init(week, rid);
        model.addAttribute("singleWeekCourse", weekCourse);
        model.addAttribute("week", week);
        return "Course/SingleCourseSchedule";
    }

    @RequestMapping("schedule")
    public String schedule(Model model){
        model.addAttribute("courseArrange",
                new CourseArrange(
                        108181414,
                        1301202007,
                        7,
                        1,
                        8,
                        1,
                        814,
                        1,
                        4
                ));
        return "Course/schedule";
    }

    public String checkInput(String input){
        if (input != null){
            return input;
        }
        return "0";
    }

    @RequestMapping("schedule_info")
    public String scheduleInfo(
            Model model,
//            CourseArrange courseArrange
            String C_ID,
            String TID,
            String S_WEEK,
            String E_WEEK,
            String DAY,
            String RID,
            String SESSION,
            String SESSION_COUNT
        ){
        // 检查传入的参数
        C_ID = checkInput(C_ID);
        TID = checkInput(TID);
        S_WEEK = checkInput(S_WEEK);
        E_WEEK = checkInput(E_WEEK);
        DAY = checkInput(DAY);
        SESSION = checkInput(SESSION);
        SESSION_COUNT = checkInput(SESSION_COUNT);

        // 将获得的参数实例化为对象
        CourseArrange courseArrange = new CourseArrange(
                0, Integer.parseInt(C_ID), Integer.parseInt(TID),
                Integer.parseInt(S_WEEK), Integer.parseInt(E_WEEK),
                Integer.parseInt(DAY), Integer.parseInt(RID),
                Integer.parseInt(SESSION), Integer.parseInt(SESSION_COUNT)
        );
        logger.info(courseArrange.toString());
        // 检查课程安排信息是否合理
        if (new ClassroomCourseArrangeImpl().checkCourse(courseArrange)) {
            // 信息不合理就返回原始页面
            logger.warn("课程信息冲突");
            model.addAttribute("courseArrange", courseArrange);
            return "Course/schedule";
        } else {
            // 信息合理，插入新的排课信息
            boolean isInsert = new ClassroomCourseArrangeImpl().insertCourse(courseArrange);
            if (isInsert) {
                // 插入信息时出现错误返回
                logger.warn("数据写入错误");
                model.addAttribute("courseArrange", courseArrange);
                return "Course/schedule";
            }
            return "Course/schedule_success";
        }
    }

}
